package GUI;

import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.AbstractAction;

import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

import javax.swing.Action;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.CloseAction;

import ControlLayer.*;
import javax.swing.ImageIcon;

public class Login {

	private JFrame Window;
	private JTextField PasswordField;
	private JTextField EmployeeIDField;
	private final Action FindEmployee = new FindEmployee();
	private PersonCtr personctr;
	private String Admin;
	private NewSalePanel newSalePanel;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.Window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
		this.Admin = "hej";
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		Window = new JFrame();
		Window.setFont(new Font("Arial", Font.PLAIN, 14));
		Window.setTitle("Login");
		Window.setBounds(100, 100, 800, 600);
		Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Window.getContentPane().setLayout(null);
		
		JLabel PasswordLabel = new JLabel("Password");
		PasswordLabel.setFont(new Font("Arial", Font.PLAIN, 14));
		PasswordLabel.setBounds(10, 536, 100, 14);
		Window.getContentPane().add(PasswordLabel);
		
		JLabel EmployeeIDLabel = new JLabel("Medarbejder ID");
		EmployeeIDLabel.setFont(new Font("Arial", Font.PLAIN, 14));
		EmployeeIDLabel.setBounds(10, 511, 100, 14);
		Window.getContentPane().add(EmployeeIDLabel);
		
		PasswordField = new JTextField();
		PasswordField.setFont(new Font("Arial", Font.PLAIN, 14));
		PasswordField.setBounds(150, 530, 86, 20);
		Window.getContentPane().add(PasswordField);
		PasswordField.setColumns(10);
		
		EmployeeIDField = new JTextField();
		EmployeeIDField.setFont(new Font("Arial", Font.PLAIN, 14));
		EmployeeIDField.setBounds(150, 505, 86, 20);
		Window.getContentPane().add(EmployeeIDField);
		EmployeeIDField.setColumns(10);
		
		JButton LoginButton = new JButton("Login");
		LoginButton.setAction(FindEmployee);
		LoginButton.setFont(new Font("Arial", Font.PLAIN, 14));
		LoginButton.setBounds(685, 527, 89, 23);
		Window.getContentPane().add(LoginButton);
		
		JLabel ImageLabel = new JLabel("");
		ImageLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		ImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
		ImageLabel.setIcon(new ImageIcon(Login.class.getResource("/Image/Test/Resources/Logo.jpg")));
		ImageLabel.setFont(new Font("Arial", Font.PLAIN, 14));
		ImageLabel.setBounds(10, 11, 764, 489);
		Window.getContentPane().add(ImageLabel);
	}
	@SuppressWarnings("serial")
	private class FindEmployee extends AbstractAction 
	{

		public FindEmployee() 
		{
			putValue(NAME, "Login");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) 
		{
			FindEmployee();
		}
		
		private void FindEmployee() 
		{
			// TODO Auto-generated method stub
			String searchText = EmployeeIDField.getText();
			String PWText = PasswordField.getText();
			
			PersonCtr personctr = new PersonCtr();
			try
			{
				if(PWText.equalsIgnoreCase(Admin))
				{
					new MainWindow().setVisible(true);
					Window.setVisible(false);
				}
				else
				{
				if(personctr.getEmployee(searchText) != null)
				{
					new MainWindow().setVisible(true);
					Window.setVisible(false);
				}
				else
				{
					JOptionPane.showMessageDialog(EmployeeIDField, "Medarbejder ikke fundet.");
				}
				}
			}
			catch(NullPointerException e)
			{
				JOptionPane.showMessageDialog(EmployeeIDField, "GZ");
			}
		}
	}
}
